package com.gjx.clock.ui


import android.os.Bundle
import android.view.Gravity
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gjx.clock.ClockViewModel
import com.gjx.clock.R
import com.gjx.clock.RestAdapter
import com.gjx.clock.dao.RestClockSettingDao
import com.gjx.clock.entity.RestAlarmClock
import com.google.android.material.floatingactionbutton.FloatingActionButton

import kotlin.collections.ArrayList
import kotlin.concurrent.thread

/**
 * A simple [Fragment] subclass.
 */
class RestAlarmClockFragment : Fragment() {

    private lateinit var adapter: RestAdapter
    private val viewModel by lazy { ViewModelProvider(this).get(ClockViewModel::class.java) }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        // 给数据库初始化几个闹钟
//        for (clock in initClock()){
//            viewModel.insertClock(clock)
//        }

        // 从数据库获取闹钟,主线程
        viewModel.loadRestClocks()

        val view = inflater.inflate(R.layout.fragment_rest_alarm_clock, container, false)
        val recyclerView = view.findViewById(R.id.restRecyclerView) as RecyclerView
        val layoutManager = LinearLayoutManager(context)
        recyclerView.layoutManager = layoutManager
        adapter = RestAdapter(viewModel, this, viewModel.restClockList,RestClockSettingDao.getSavedClock())
        recyclerView.adapter = adapter

        val addRestBtn = view.findViewById<FloatingActionButton>(R.id.addRestBtn)

        // 添加闹钟
        addRestBtn.setOnClickListener {
            val contentView = LayoutInflater.from(context).inflate(R.layout.rest_clock_settings, null)
            val popWnd = PopupWindow (activity)
            popWnd.contentView = contentView
            popWnd.width = ViewGroup.LayoutParams.MATCH_PARENT
            popWnd.height = ViewGroup.LayoutParams.WRAP_CONTENT
            popWnd.isFocusable=true

            val workingTimeEditText = contentView.findViewById<NumberPicker>(R.id.workingTimeSetting)
            val restTimeEditText = contentView.findViewById<NumberPicker>(R.id.restTimeSetting)
            workingTimeEditText.minValue=1
            workingTimeEditText.maxValue=180

            restTimeEditText.minValue=1
            restTimeEditText.maxValue=180

            val deleteBtn = contentView.findViewById<ImageButton>(R.id.deleteRestBtn)
            val saveButton = contentView.findViewById<Button>(R.id.saveRestBtn)
            val cancelButton = contentView.findViewById<Button>(R.id.cancelRestBtn)

            workingTimeEditText.value=60
            restTimeEditText.value=10

            // 保存闹钟
            saveButton.setOnClickListener {
                val workingTime = workingTimeEditText.value
                val restTime = restTimeEditText.value
                val clock = RestAlarmClock(workingTime,restTime,false)

                thread {
                    // 创建的clock对象的id还没和数据库同步呢，所以要更新这个id
                    viewModel.insertClock(clock)
                    activity?.runOnUiThread{
                        adapter.notifyDataSetChanged()
                    }
                }
                popWnd.dismiss()
            }
            // 取消设置
            cancelButton.setOnClickListener {
                popWnd.dismiss()
            }
            //没有删除选项
            deleteBtn.visibility = View.GONE
            popWnd.showAtLocation(it, Gravity.CENTER,0,0)
        }
        return view
    }

    private fun initClock():List<RestAlarmClock> {
        val clockList = ArrayList<RestAlarmClock>()
        clockList.apply {
            add(RestAlarmClock(120, 15, true))
            add(RestAlarmClock(60, 10, false))
            add(RestAlarmClock(30, 5, false))
        }
        return clockList
    }
}
